package com.cloud.shopping.druid.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.shopping.druid.mapper.HotGoodsMapper;
import com.cloud.shopping.druid.model.HotGoods;
import com.cloud.shopping.druid.service.HotGoodsService;
import com.cloud.shopping.druid.util.DruidPage;
import com.cloud.shopping.druid.util.TimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author John.zhang
 * @version Id: HotGoodsServiceImpl, v 1.0 2021/2/8 9:36 ZhiYong Exp $
 */

@Service
public class HotGoodsServiceImpl extends ServiceImpl<HotGoodsMapper, HotGoods> implements HotGoodsService {

    @Resource
    private HotGoodsMapper hotGoodsMapper;

    @Override
    public List<Map<String, String>> searchHotGoods(Integer size, Integer hour, String[] urls, Integer max) {
        String allUrls = StringUtils.join(urls,"','");
        return hotGoodsMapper.searchHotGoods(size, TimeUtil.beforeTime(TimeUtil.UNIT_HOUR,hour),allUrls,max);
    }

    @Override
    public List<HotGoods> search(Integer size, Integer hour, String[] urls) {
        String allUrls = StringUtils.join(urls,"','");
        return hotGoodsMapper.searchExclude(size, TimeUtil.beforeTime(TimeUtil.UNIT_HOUR,hour),allUrls);
    }

    @Override
    public List<HotGoods> search(Integer size, Integer hour) {
        return hotGoodsMapper.search(size, TimeUtil.beforeTime(TimeUtil.UNIT_HOUR,hour));
    }

    @Override
    public DruidPage<List<HotGoods>> pageListSort(Integer size, Integer page, String sort, String sortType) {
        //计算偏移量
        DruidPage<List<HotGoods>> druidPage = new DruidPage<>(page, size, sort, sortType);
        //查询总数
        Integer total = hotGoodsMapper.selectCount(null);
        //查询集合
        List<HotGoods> hotGoods = hotGoodsMapper.pageListSort(druidPage);
        return druidPage.pages(hotGoods,total);
    }

    @Override
    public DruidPage<List<HotGoods>> pageList(Integer size, Integer page) {
        //计算偏移量
        DruidPage<List<HotGoods>> druidPage = new DruidPage<>(page, size);
        //查询总数
        Integer total = hotGoodsMapper.selectCount(null);
        //查询集合
        List<HotGoods> hotGoods = hotGoodsMapper.pageList(size, druidPage.getOffset());
        return druidPage.pages(hotGoods,total);
    }

    @Override
    public List<HotGoods> topNum(Integer size) {
        return hotGoodsMapper.topNum(size);
    }
}
